
package com.android.fitnesscenter.view.server;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.android.fitnesscenter.R;
import com.android.fitnesscenter.utils.ConfigProperties.RoleType;
import com.android.fitnesscenter.utils.ToastHelper;
import com.android.fitnesscenter.view.BaseActivity;
import com.android.fitnesscenter.widget.SwitchButton;
import com.android.fitnesscenter.widget.SwitchButton.OnCheckedChangeListener;
import com.lidroid.xutils.ViewUtils;

public class ServerSettingActivity extends BaseActivity implements OnClickListener {
    private SwitchButton switch_auto_login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.setting_layout);
        ViewUtils.inject(this);
        initTitleBar(getString(R.string.txt_setting));
        initView();
    }

    private void initView() {
        initIncludeView(R.id.include_limit_setting, -1, getString(R.string.txt_limit_setting), false);
        initIncludeView(R.id.include_modify_password, R.drawable.icon_permission,
                getString(R.string.txt_permission_setting));
        initIncludeView(R.id.include_backup_setting, R.drawable.icon_backup, getString(R.string.txt_setting_backup));
        initIncludeView(R.id.include_about_us, R.drawable.icon_about, getString(R.string.txt_setting_about));
        switch_auto_login = (SwitchButton) findViewById(R.id.switch_auto_login);
        boolean isAuto = mSharePrefsHelper.getBoolean("isAuto", false);
        switch_auto_login.setChecked(isAuto);
        switch_auto_login.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void checkedChange(boolean isChecked) {
                mSharePrefsHelper.setBoolean("isAuto", isChecked);
            }
        });
    }

    private void initIncludeView(int layoutId, int id, String title) {
        initIncludeView(layoutId, id, title, true);
    }

    private void initIncludeView(int layoutId, int id, String title, boolean isShow) {
        RelativeLayout include_setting = (RelativeLayout) findViewById(layoutId);
        if (id != -1) {
            ImageView img_icon = (ImageView) include_setting.findViewById(R.id.img_icon);
            img_icon.setVisibility(View.VISIBLE);
            img_icon.setImageResource(id);
        }
        if (isShow) {
            TextView txt_title = (TextView) include_setting.findViewById(R.id.txt_title);
            txt_title.setText(title);
            include_setting.setOnClickListener(this);
            include_setting.setVisibility(View.VISIBLE);
        } else {
            include_setting.setVisibility(View.GONE);
        }
    }

    @Override
    protected void titlebarMethod(int id) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.include_limit_setting:
                break;
            case R.id.include_modify_password:
                String qx4 = mSharePrefsHelper.getString("qx4", "0");
                if (TextUtils.equals(qx4, "1")) {
                    Intent intentPermission = new Intent(this, ServerPermissionActivity.class);
                    startActivity(intentPermission);
                } else {
                    ToastHelper.getInstance().showShortMsg("没有权限,请联系管理员!");
                }
                break;
            case R.id.include_backup_setting:
                String roleType = mSharePrefsHelper.getString("role_type", RoleType.MANAGER);
                if (TextUtils.equals(roleType, RoleType.MANAGER)) {
                    Intent intent = new Intent(this, ServerBackupActivity.class);
                    startActivity(intent);
                } else {
                    ToastHelper.getInstance().showShortMsg("没有权限,请联系管理员!");
                }
                break;
            case R.id.include_about_us:
                Intent intentAbout = new Intent(ServerSettingActivity.this, ServerAboutActivity.class);
                startActivity(intentAbout);
                break;
        }
    }

}
